package com.hyperf.event.client.impl;

import cn.hutool.core.util.StrUtil;
import com.hyperf.event.client.EventListenerService;
import com.hyperf.event.client.EventMessageListener;
import com.hyperf.event.client.exception.EventException;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lizhaoyang
 */
public abstract class AbstractEventListenerService implements EventListenerService {

    protected final Map<String, EventMessageListener> eventListenerMap = new ConcurrentHashMap<>();

    @Override
    public void addListener(EventMessageListener listener) {
        String eventType = listener.getEventType();
        if (StrUtil.isEmpty(eventType)) {
            throw new EventException("listener event type can not be empty");
        } else if (eventListenerMap.containsKey(eventType)) {
            throw new EventException("listener event type must be unique, " + eventType + " has exist in " + eventListenerMap.get(eventType).getClass().getName());
        }

        eventListenerMap.put(eventType, listener);
    }
}
